package invoicesystem.util;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;

public class DBHelper {

    //1+2
    public Connection getCon() {
        Connection con = null;
        try {

            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://127.0.0.1:3306/invoicesystem?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true";
            String username = "root";
            String password = "root";
            con = DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            //e.printStackTrace();
            System.out.println("mysql 驱动类地址不对，没有导入jar，url路径不对。");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    //7.
    public void closeAll(Connection con, PreparedStatement ps, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //1-7查询汇总，传入3，返回6
    public ArrayList selectinfo(String sql,Class cla,Object...objs){
        ArrayList list=new ArrayList();
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            con = getCon();
            ps = con.prepareStatement(sql);
            for (int i = 0; i < objs.length; i++) {
                ps.setObject(i + 1, objs[i]);
            }
            rs = ps.executeQuery();
            while (rs.next()) {
               // 1.根据cla，获得字节码对应类型下的对象：//
                Object o = cla.newInstance();
                //2.给对象属性赋值：
                Field[] declaredField = cla.getDeclaredFields();
                for (Field f : declaredField) {
                    f.setAccessible(true);
                    f.set(o, rs.getObject(f.getName()));
                }
                list.add(o);
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(  con, ps, rs);
        }
        return list;
    }

    //求符合条件的数据总个数：
    public int selectinfoCount(String sql,Object...objs){
        int count=0;
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            con = getCon();
            ps = con.prepareStatement(sql);
            for (int i = 0; i < objs.length; i++) {
                ps.setObject(i + 1, objs[i]);
            }
            rs = ps.executeQuery();
            if(rs.next()){
                count=rs.getInt(1);
            }
        }catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(  con, ps, rs);
        }
        return count;
    }


    public int updateinfo(String sql,Object...objs){
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        int count=0;
        try {
            con = getCon();
            ps = con.prepareStatement(sql);
            for (int i = 0; i < objs.length; i++) {
                ps.setObject(i + 1, objs[i]);
            }
            count= ps.executeUpdate();
        }  catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(  con, ps, rs);
        }
        return count;
    }


}
